package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.AssetRecordAdapter;
import com.wnhz.work.jy.bean.AssetItemBean;
import com.wnhz.work.jy.bean.AssetRecordBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---资产记录
 */
public class AssetRecordActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ListView lv_asset;
    private List<AssetRecordBean> list = new ArrayList<>();
    private AssetRecordAdapter mAdapter;
    @ViewInject(R.id.include_occupied)
    private View include_occupied;
    @ViewInject(R.id.blank_img)
    private ImageView blank_img;
    @ViewInject(R.id.blank_tv)
    private TextView blank_tv;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asset_record);
        x.view().inject(this);
        initData();
        initView();
        setBlank();
    }

    private void setBlank() {
        if (list.size() == 0) {
            blank_tv.setText("还没有该状态下的记录哦~");
            blank_img.setImageResource(R.mipmap.ic_kongdingdan);
            include_occupied.setVisibility(View.VISIBLE);
        } else {
            include_occupied.setVisibility(View.GONE);
        }
    }

    private void initData() {

    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("资产记录");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        lv_asset = (ListView) findViewById(R.id.lv_asset);

        uploadByxUtils();

        mAdapter = new AssetRecordAdapter(this, list);
        lv_asset.setAdapter(mAdapter);
    }

    private static final String TAG = "AssetRecordActivity";
    private AssetRecordBean assetBean;

    private void uploadByxUtils() {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.ASSETSRECORD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: AssetRecordActivity===" + result.substring(0, result.length() / 2));
                Log.i(TAG, "onSuccess: AssetRecordActivity===" + result.substring(result.length() / 2, result.length()));
                list.clear();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("assetsRecordArr");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        assetBean = new AssetRecordBean();

                        assetBean = gson.fromJson(jsonArray.optString(i), AssetRecordBean.class);
                        assetBean.setMyType(1);
                        list.add(assetBean);
                    }
                    JSONArray tackDataArr = jsonObject.optJSONArray("tackDataArr");
                    if (tackDataArr != null) {
                        for (int i = 0; i < tackDataArr.length(); i++) {
                            assetBean = new AssetRecordBean();

                            assetBean = gson.fromJson(tackDataArr.optString(i), AssetRecordBean.class);
                            assetBean.setMyType(2);
                            JSONArray goodsInfo = tackDataArr.optJSONObject(i).optJSONArray("goodsInfo");
                            ArrayList<AssetItemBean> assetItemBeans = new ArrayList<>();
                            for (int i1 = 0; i1 < goodsInfo.length(); i1++) {
                                AssetItemBean assetItemBean = new AssetItemBean();
                                assetItemBean = gson.fromJson(goodsInfo.optString(i1), AssetItemBean.class);
                                assetItemBeans.add(assetItemBean);
                            }
                            assetBean.setOrderDetail(assetItemBeans);
                            list.add(assetBean);
                        }
                    }
                    JSONArray sellDataArr = jsonObject.optJSONArray("sellDataArr");
                    if (sellDataArr != null) {
                        for (int i = 0; i < sellDataArr.length(); i++) {
                            assetBean = new AssetRecordBean();

                            assetBean = gson.fromJson(sellDataArr.optString(i), AssetRecordBean.class);
                            assetBean.setMyType(3);
                            List<AssetItemBean> orderDetail = new ArrayList<>();
                            AssetItemBean assetItemBean = new AssetItemBean();

                            assetItemBean = gson.fromJson(sellDataArr.optString(i), AssetItemBean.class);
                            orderDetail.add(assetItemBean);
                            assetBean.setOrderDetail(orderDetail);
                            list.add(assetBean);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mHandler.sendEmptyMessage(1);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
                setBlank();
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }
}
